home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / appkit.613 < prev    next >
Text File  |  1992-02-06  |  3KB  |  74 lines

  1. {\rtf0\ansi{\fonttbl\f2\fnil Times-Roman;\f3\fmodern Courier;\f0\fswiss Helvetica;}
  2. \paperw11440
  3. \paperh9000
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f2\b0\i0\ul0\fs28 Text fixed width window\
  8.     \
  9. Q:  How do I programmatically create a window 80 characters wide for any given font?\
  10.  
  11. \i     \
  12.  
  13. \i0 A:  The following code snippet will work for a fixed width font such as Ohlfs or Courier. A non-fixed width font can only be approximated by choosing either an "average-width" character, or the widest character in the font.\
  14. \
  15. \
  16.  
  17. \f3\fs24 #import <math.h>\
  18. #import <appkit/Window.h>\
  19. #import <appkit/Font.h>\
  20. \
  21. float        maxcharwidth, screenwidth;\
  22. id         screenFont, myWindow;\
  23. NXCoord    leftMargin, rightMargin, topMargin, bottomMargin;\
  24. NXRect        aRect;\
  25. \
  26.     [myText getMarginLeft:  &leftMargin\
  27.               right:  &rightMargin\
  28.               top:    &topMargin\
  29.               bottom: &bottomMargin];\
  30.     screenFont = [myFont screenFont];\
  31. \
  32.     maxcharwidth = MAX([myFont getWidthOf: "0"], \
  33.                   [screenFont getWidthOf: "0"]);\
  34.     screenwidth = ceil(maxcharwidth * 80.0) + leftMargin + rightMargin +1;\
  35. \
  36.     /* Now use that width when creating the window */\
  37.     NXSetRect(&aRect, 0.0, 0.0, screenwidth, height);\
  38.  
  39. \b #ifdef 1.0\
  40.  
  41. \b0     myWindow = [Window newContent: &aRect\
  42.             style: NX_TITLEDSTYLE\
  43.             backing: NX_BUFFERED\
  44.             buttonMask: NX_MINIATURIZEBUTTONMASK\
  45.             defer: NO];\
  46.  
  47. \pard\tx622\tx1245\tx1868\tx2490\tx3113\tx3736\tx4359\tx4981\tx5604\tx6227\f2\b\fs28\fc0 #else /* 2.0 */\
  48.  
  49. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f3\b0\fs24     myWindow = [[Window alloc] initContent: &aRect\
  50.             style: NX_TITLEDSTYLE\
  51.             backing: NX_BUFFERED\
  52.             buttonMask: NX_MINIATURIZEBUTTONMASK\
  53.             defer: NO];\
  54.  
  55. \pard\tx622\tx1245\tx1868\tx2490\tx3113\tx3736\tx4359\tx4981\tx5604\tx6227\f2\b\fs28\fc0 #endif\
  56.  
  57. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\b0 \
  58.  
  59. \b Note that the ifdef for 1.0 is for illustration purposes only and is NOT defined in any NeXTstep include file.\
  60.  
  61. \b0 \
  62. The key is to get the width of any character (since all have the same width) of both the screen font and the printer font.  Take the maximum width and do a ceil of the calculation, since the printer width may be fractional.\
  63. \
  64. Note that a fudge factor of +1 is added to the calculation.  This is a known bug.\
  65. \
  66. Note also that this window does not contain a vertical scrollbar.  If it did, the width of the scrollbar would need to be accounted for.\
  67. \
  68. QA613    \
  69. \
  70. Valid for 1.0\
  71. Valid for 2.0  (with the differences noted)        \
  72. \
  73.  
  74.